package dao.impl;

import dao.PetStoreDao;
import entity.PetOwner;
import entity.PetStore;

import java.util.ArrayList;
import java.util.List;

public class PetStoreDaoImpl extends BaseDao implements PetStoreDao {
    @Override
    public List<PetStore> getPetStoreList() throws Exception {

        connection=getConnection();
        String sql="select * from petstore";
        resultSet = query(sql, null);
        ArrayList<PetStore> list = new ArrayList<>();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String password = resultSet.getString("password");
            int balance = resultSet.getInt("balance");

            PetStore petStore = new PetStore();
            petStore.setId(id);
            petStore.setName(name);
            petStore.setBalance(balance);
            petStore.setPassword(password);
            //添加到集合
            list.add(petStore);
        }
        close(resultSet,statement,connection);
        return list;
    }

    @Override
    public int login(PetStore petStore) throws Exception{
        connection=getConnection();
        String sql="select * from petStore where id=? and name=?";
        Object[] objects = new Object[2];
        objects[0]=petStore.getId();
        objects[1]=petStore.getName();
        int update = update(sql, objects);
        close(resultSet,statement,connection);
        return update;
    }
//卖
    @Override
    public int getBuy(Integer storeId, Integer price) throws Exception{
        connection=getConnection();
       String sql="update petstore set balance=balance+? where id=? ";
       Object[] objects={price,storeId};
        int update = update(sql, objects);
        close(resultSet,statement,connection);
        return update;
    }
//买
    @Override
    public int changeStore(Integer storeId, Integer price) throws Exception{
        connection=getConnection();
        String sql="update petstore set balance=balance-? where id=? ";
        Object[] objects={price,storeId};
        int update = update(sql, objects);
        close(resultSet,statement,connection);
        return update;
    }
}
